package com.hxd.buybooksystem.adapter

import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.hjq.image.ImageLoader
import com.hxd.buybooksystem.R
import com.hxd.buybooksystem.bean.BookInfo

/**
 *  项目名：    BuyBookSystem
 *  包名：      com.hxd.buybooksystem.adapter
 *  文件名:     IndexBookAdapter
 *  创建者:     HXD
 *  创建时间:   2019/6/1 15:23
 *  描述:       TODO
 */

class IndexBookAdapter(val datas:ArrayList<BookInfo>):RecyclerView.Adapter<IndexBookAdapter.ViewHolder>(){

    private var mOnItemClickLitener: OnItemClickLitener? = null

    //设置回调接口
    interface OnItemClickLitener {
        fun onItemClick(position:Int, bookInfo: BookInfo)
    }

    fun setOnItemClickLitener(mOnItemClickLitener: OnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_book, parent, false)
        return ViewHolder(view)
    }

    override fun getItemCount(): Int {
        return datas.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val bookInfo = datas[position]
        ImageLoader.loadImage(holder.bookImage, bookInfo.image)
        holder.bookName.text = bookInfo.name
        holder.bookImage.setOnClickListener {
            mOnItemClickLitener!!.onItemClick(position, bookInfo)
        }
    }


    /**
     * RecyclerView的持有者类
     */
    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var bookImage: ImageView = view.findViewById(R.id.book_image)
        var bookName: TextView = view.findViewById(R.id.book_name)

    }



}